package com.cuhk.gulimall.product.web;


import com.cuhk.gulimall.product.entity.CategoryEntity;
import com.cuhk.gulimall.product.service.CategoryService;
import com.cuhk.gulimall.product.vo.CategoryLevel2Vo;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;

    @GetMapping({"/", "/index.html"})
    public ModelAndView indexPage(Model model) {
        // TODO 查询出所有的一级分类
        List<CategoryEntity> firstLevelCategories = categoryService.getLevel1Categories();
        ModelAndView modelAndView = new ModelAndView("index");
        // model.addAttribute("firstLevelCategories", firstLevelCategories);
        modelAndView.addObject("firstLevelCategories", firstLevelCategories);
        return modelAndView;
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<CategoryLevel2Vo>> getCatalogJson() {
        // Map<String, List<CategoryLevel2Vo>> catalogJson = categoryService.getCatalogJsonFromDB();
        // redis缓存存储优化
        // Map<String, List<CategoryLevel2Vo>> catalogJson = categoryService.getCatalogJsonFromRedis();
        // 使用注解话配置缓存
        Map<String, List<CategoryLevel2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        RLock lock = redisson.getLock("my-lock");
        // 加锁
        try {
            lock.lock();
            System.out.println("加锁成功, 执行业务...");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("释放锁....");
            lock.unlock();
        }
        return "/";
    }

    @GetMapping("/read")
    @ResponseBody
    public String read () {
        RReadWriteLock rwLock = redisson.getReadWriteLock("lock-test-01");
        // 获取读锁
        RLock readLock = rwLock.readLock();
        try {
            readLock.lock();
            System.out.println("读线程获取锁...");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
        return "/";
    }

    @GetMapping("/write")
    @ResponseBody
    public String write () {
        RReadWriteLock rwLock = redisson.getReadWriteLock("lock-test-01");
        RLock writeLock = rwLock.writeLock();
        try {
            writeLock.lock();
            System.out.println("写线程获取锁");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
        return "/";
    }

    @GetMapping("/lock-door")
    @ResponseBody
    public String lockDoot() {
        RCountDownLatch doorLock = redisson.getCountDownLatch("door-lock");
        doorLock.trySetCount(5);
        try {
            System.out.println("锁门线程等待...");
            doorLock.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "/";
    }

    @GetMapping("/after-school")
    @ResponseBody
    public String afterSchool() {
        RCountDownLatch doorLock = redisson.getCountDownLatch("door-lock");
        System.out.println("走了一个...");
        doorLock.countDown();
        return "/";
    }
}
